package com.kingmed.kmss.bpmn.flow.utils;

import java.util.ArrayList;
import java.util.List;

import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;

import com.kingmed.kmss.common.bo.bpmn.DeploymentBO;
import com.kingmed.kmss.common.bo.bpmn.GroupBO;
import com.kingmed.kmss.common.bo.bpmn.ProcessDefinitionBO;
import com.kingmed.kmss.common.bo.bpmn.UserBO;
import com.kingmed.kmss.common.utils.BeanCopyUtils;
import com.kingmed.kmss.common.utils.StringUtils;

/**
 * Bean util无法copy flowable的实体类，放到这里转换
 *
 *
 */
public class FlowableBeanUtils {
	public static UserBO toUserBO(User user) {
		UserBO bo = null;
		
		if (user != null) {
			bo = new UserBO();
			bo.setEmail(user.getEmail());
			bo.setName(user.getDisplayName());
			//bo.setGroupId(user.getGroupId());
			//bo.setGroupName(null);
			//bo.setGroupType(null);
			bo.setUserId(user.getId());
		}
		
		return bo;
	}
	
	public static GroupBO toGroupBO(Group group) {
		GroupBO bo = null;
		
		if (group != null) {
			bo = new GroupBO();
			bo.setGroupId(group.getId());
			bo.setName(group.getName());
			bo.setType(group.getType());
		}
		
		return bo;
	}
	public static List<UserBO> toUserBO(List<User> users) {
		if (users == null || users.size() == 0) {
			return null;
		}
		List<UserBO> result = new ArrayList<UserBO>();
		for (User user : users) {
			result.add(toUserBO(user));
		}
		return result;
	}
	
	public static List<GroupBO> toGroupBO(List<Group> groups) {
		if (groups == null || groups.size() == 0) {
			return null;
		}
		List<GroupBO> result = new ArrayList<GroupBO>();
		for (Group entity : groups) {
			result.add(toGroupBO(entity));
		}
		return result;
	}
	

	public static List<ProcessDefinitionBO> toProcessDefinitionBO(List<ProcessDefinition> list) {
		List<ProcessDefinitionBO> result = new ArrayList<>();
        if (!list.isEmpty()) {
            for (ProcessDefinition definition : list) {
                ProcessDefinitionBO item = new ProcessDefinitionBO();
                BeanCopyUtils.copy(definition, item);
                //model和bo，dto之间，字段设计时最好保持一致
                item.setProcessKey(definition.getKey());
                item.setProcessId(definition.getId());
                item.setProcessName(definition.getName());
                item.setCategory(definition.getCategory());
                item.setDeploymentId(definition.getDeploymentId());
                item.setResourceName(getResoureFileName(definition.getResourceName()));
                item.setVersion(definition.getVersion());
                result.add(item);
            }
        }
		return result;
	}


    
    public static String getResoureFileName(String resourceName) {
    	String filename = resourceName;

    	if (StringUtils.isEmpty(resourceName)) {
			return resourceName;
		}
		
		// 去掉路径，只留文件名
		int pos = resourceName.lastIndexOf("\\"); // windows
		if (pos == -1) {
			pos = resourceName.lastIndexOf("/"); // linux
		}
		
		if (pos != -1) {
			filename = resourceName.substring(pos + 1);
		}
		
		return filename;
	}
    
	public static List<DeploymentBO> toDeploymentBO(List<Deployment> list) {
		List<DeploymentBO> result = new ArrayList<>();
        if (!list.isEmpty()) {
            for (Deployment deploy : list) {
            	DeploymentBO item = toDeploymentBO(deploy);
                
                result.add(item);
            }
        }
		return result;
	}

	public static DeploymentBO toDeploymentBO(Deployment deploy) {
		DeploymentBO item = new DeploymentBO();
		BeanCopyUtils.copy(deploy, item);
            
		item.setId(deploy.getId());
		item.setName(deploy.getName());
		item.setCategory(deploy.getCategory());
		item.setDeployTime(deploy.getDeploymentTime());
		return item;
	}
}
